package com.sysolve.betterCity.service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class DailyScheduler extends TimerTask {
	static DateFormat DATETIME = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	static DateFormat DATE = new SimpleDateFormat("yyyy-MM-dd");
	
	public static CalcHotArea calc;
	
	public void run() {
		calc.startCalc();
	    System.out.println("Run at "+DATETIME.format(new Date()));
	}

    public static boolean schedule(CalcHotArea c) { 
    	calc = c;
    	
    	Date date = new Date();  
    	Date base = new Date();
		try {
			base = new Date(DATE.parse(DATE.format(date)).getTime());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	System.out.println("Start at "+DATETIME.format(base)); 
    	
    	date = new Date(date.getTime()+60*60*1000);
    	while (date.after(base)) base = new Date(base.getTime()+6*60*60*1000);
    	System.out.println("Schedule at "+DATETIME.format(base));
    	new Timer(false).schedule(new DailyScheduler(), base, 6*60*60*1000);
    	
    	return true;
    } 
}
